package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.dto.request.PartnerGradListeDto;
import com.huaxiafinance.management.dto.request.PartnerGradeDto;
import com.huaxiafinance.management.dto.request.PartnerGradeSortDto;
import com.huaxiafinance.management.dto.response.PartnerWithGrade;
import com.huaxiafinance.management.mapper.PartnerConditionMapper;
import com.huaxiafinance.management.mapper.PartnerGradeMapper;
import com.huaxiafinance.management.mapper.UserMapper;
import com.huaxiafinance.management.mapper.YfqLogMapper;
import com.huaxiafinance.management.model.PartnerCondition;
import com.huaxiafinance.management.model.PartnerGrade;
import com.huaxiafinance.management.model.User;
import com.huaxiafinance.management.model.YfqLog;
import com.huaxiafinance.management.service.PartnerGradeService;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author yangtong
 * @create 2017-12-06 11:07
 **/
@Service
@SuppressWarnings("SpringJavaAutowiringInspection")
public class PartnerGradeServiceImpl implements PartnerGradeService {
  private static final Logger log = Logger.getLogger(PartnerGradeServiceImpl.class);

  @SuppressWarnings("SpringJavaAutowiringInspection")
  @Autowired
  private PartnerGradeMapper partnerGradeMapper;

  @Autowired
  private PartnerConditionMapper partnerConditionMapper;

  @Autowired
  private UserMapper userMapper;

  @Override
  public List<PartnerWithGrade> getAllPartnerGrade() {
    List<PartnerGrade> partnerGrades = partnerGradeMapper.selectAllPartnerGrade();

    List<PartnerWithGrade> partnerWithGrades = new ArrayList<PartnerWithGrade>();
    for (PartnerGrade partnerGrade : partnerGrades) {
      PartnerWithGrade partnerWithGrade = new PartnerWithGrade();
      partnerWithGrade.setId(partnerGrade.getId());
      partnerWithGrade.setName(partnerGrade.getGradeName());
      partnerWithGrades.add(partnerWithGrade);

    }
    PartnerWithGrade partnerWithGrade = new PartnerWithGrade();
    partnerWithGrade.setId(0);
    partnerWithGrade.setName("无等级");
    partnerWithGrades.add(partnerWithGrade);
    return partnerWithGrades;
  }

  @Override
  public PartnerGrade getPartnerGradeByPrimaryKey(Integer id) {
    if (id != null) {
      return partnerGradeMapper.selectByPrimaryKey(id);
    } else {
      return null;
    }
  }

  @Autowired
  private YfqLogMapper yfqLogMapper;

  private void insertLog(Integer adminid, String message) {

    if (adminid == null || message == null) {
      return;
    }

    try {
      YfqLog log = new YfqLog();
      log.setCreateAt(new Date());

      log.setLogType(2);
      log.setAdminId(adminid);
      log.setMessage(message);

      yfqLogMapper.insert(log);
    } catch (Exception ex) {
      log.info("错误++++++++" + ex);
    }

  }

  @Override
  public int add(PartnerGradeDto dto) {

    PartnerGrade bo = new PartnerGrade();

    /**
     * if(isMust){
     if(value == null || value.trim().isEmpty()){
     String msg = name + Message.ERROR_002;
     throw new BizException(msg);
     }
     }
     List<T> list = JSONArray.parseArray(value, clazz);
     if(isMust && (list == null || list.size() == 0)){
     String msg = name + Message.ERROR_002;
     throw new BizException(msg);
     }
     if(list == null){
     list = new ArrayList<T>();
     }
     return list;
     * */
    List<PartnerCondition> list = dto.getStrconditionList();
    BeanUtils.copyProperties(dto, bo);
    bo.setGmtCreate(new Date());
    bo.setId(null);
    bo.setGradeSequence(0);
    bo.setIsEnable(0);

    int temp = partnerGradeMapper.insert(bo);
    if (temp != 0) {
      //查询最后一条
      bo = partnerGradeMapper.selectLastId();

      insertLog(bo.getCreatorId(), "新建等级规则：" + bo.getId());

      for (int i = 0; list != null && i < list.size(); i++) {
        list.get(i).setPartnerGradeId(bo.getId());
        partnerConditionMapper.insertSelective(list.get(i));
      }
    }
    return temp;

  }

  @Override
  public int edit(PartnerGradeDto dto) {
    PartnerGrade bo = new PartnerGrade();

    BeanUtils.copyProperties(dto, bo);

    bo.setGradeSequence(null);
    bo.setGmtModified(new Date());
    bo.setCreatorId(null);
    bo.setGmtCreate(null);

    List<PartnerCondition> list = dto.getStrconditionList();

    int temp = partnerGradeMapper.updateByPrimaryKeySelective(bo);
    insertLog(bo.getModifierId(), "修改等级规则：" + bo.getId());
    if (temp != 0) {
      partnerConditionMapper.deleteByPartnerGradeId(bo.getId());
      for (int i = 0; i < list.size(); i++) {
        list.get(i).setPartnerGradeId(bo.getId());
        partnerConditionMapper.insertSelective(list.get(i));
      }
    }

    return temp;
  }

  @Override
  public PartnerGrade detail(Integer gradeId) {
    PartnerGrade bo = partnerGradeMapper.selectByPrimaryKey(gradeId);
    if (bo.getCreatorId() != null) {
      User temp = userMapper.selectByPrimaryKey(bo.getCreatorId());
      if (temp != null) {
        bo.setCreatorName(temp.getName());
      }
    }
    if (bo.getModifierId() != null) {
      User temp = userMapper.selectByPrimaryKey(bo.getModifierId());
      if (temp != null) {
        bo.setModifierName(temp.getName());
      }
    }

    List<PartnerCondition> list = partnerConditionMapper.selectByPartnerGradeId(bo.getId());
    bo.setPartnerConditionList(list);
    return bo;
  }

  @Override
  public List<PartnerGrade> list(PartnerGradListeDto dto) {
    if (dto.getPage() != null && dto.getRows() != null) {
      dto.setPage(dto.getPage() * dto.getRows());
    }

    List<PartnerGrade> list = partnerGradeMapper.selectList(dto);
    return list;
  }

  @Override
  public boolean updateSort(PartnerGradeSortDto ids) {

    if (ids != null) {
      List<PartnerGrade> list = ids.getIds();
      if (list != null && list.size() > 0) {
        for (int i = 0; i < list.size(); i++) {
          PartnerGrade temp = new PartnerGrade();
          temp.setId(list.get(i).getId());
          temp.setGradeSequence(list.get(i).getGradeSequence());
          temp.setModifierId(ids.getUserId());
          temp.setGmtModified(new Date());
          if (list.get(i).getId() != null && list.get(i).getGradeSequence() != null) {
            partnerGradeMapper.updateByPrimaryKeySelective(temp);
          }
        }
      }
    }

    if (ids != null) {
      insertLog(ids.getUserId(), "修改等级规则排序");
    }

    return true;
  }

  @Override
  public int countlist(PartnerGradListeDto dto) {
    return partnerGradeMapper.getCount(dto);
  }

  @Override
  public int updateEnable(Integer gradeId, Integer isEnable, Integer userId) {
    PartnerGrade bo = new PartnerGrade();
    bo.setId(gradeId);
    bo.setIsEnable(isEnable);
    bo.setGmtModified(new Date());
    bo.setModifierId(userId);
    insertLog(bo.getModifierId(), "修改等级规则启用禁用：" + gradeId);
    return partnerGradeMapper.updateByPrimaryKeySelective(bo);
  }
}
